package com.cyw.note.controller;

import com.cyw.common.core.domain.R;
import com.cyw.common.utils.StringUtils;
import com.cyw.note.utils.EmailUtil;
import com.cyw.note.utils.IpAddressUtil;
import com.cyw.note.vo.ProposeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Chenyw
 * @Date: 2022/08/26/10:45
 */
@RestController
@RequestMapping("/note")
public class ProposeController {
    @Autowired
    private EmailUtil emailUtil;

    @PostMapping("propose")
    public R propose(@RequestBody ProposeVo proposeVo, HttpServletRequest request) {
        if (StringUtils.isEmpty(proposeVo.getEmail()) || StringUtils.isEmpty(proposeVo.getContent())) {
            return R.fail("请检查格式");
        }
        //TODO:存数据库

//        emailUtil.sendMail("1099598733@qq.com",proposeVo.getContent());
//        emailUtil.sendMail(proposeVo.getEmail(),"您的留言我们已经收到,有问题我们会及时处理");

        proposeVo.setIp(IpAddressUtil.getAddrByIp(IpAddressUtil.getIpAddr(request)));
        System.out.println(proposeVo);

        return R.ok("留言成功");
    }
}
